package com.compass.doctor.ui.home.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.compass.doctor.R
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.databinding.LayoutConsumableOrderItemBinding
import com.compass.framework.adapter.BaseBindViewHolder
import com.compass.framework.adapter.BaseRecyclerViewAdapter
import com.compass.framework.ext.gone
import com.compass.framework.ext.onClick
import com.compass.framework.model.ConsumableOrder
import com.compass.framework.model.MedicalConsumable
import com.compass.framework.utils.BigDecimalUtils
import com.compass.framework.utils.getColorFromResource

/**
 * Description： 额外耗材订单adapter
 * Date:2024/3/7 18:57
 */
class ConsumableOrderAdapter :
    BaseRecyclerViewAdapter<ConsumableOrder, LayoutConsumableOrderItemBinding>() {

    private var onCustomBottomClick: OnCustomButtonClick? = null

    fun setCustomBottomClick(onCustomBottomClick: OnCustomButtonClick?) {
        this.onCustomBottomClick = onCustomBottomClick
    }

    override fun onBindDefViewHolder(
        holder: BaseBindViewHolder<LayoutConsumableOrderItemBinding>,
        item: ConsumableOrder?,
        position: Int
    ) {
        if (item == null) return
        holder.binding.apply {

            when (item.status) {
                "1" -> {
                    tvStatus.text = "已支付"
                    tvDelete.gone()
                    tvWxCode.gone()
                    tvStatus.setTextColor(getColorFromResource(R.color.color_2e344a))
                }

                "0" -> {
                    tvStatus.text = "待支付"
                    tvStatus.setTextColor(getColorFromResource(R.color.color_ff4e32))
                }

                "-1" -> {
                    tvDelete.gone()
                    tvWxCode.gone()
                    tvStatus.setTextColor(getColorFromResource(R.color.preview_color_grey))
                    tvStatus.text = "已退款"
                }
            }

            tvName.text = getTitle(item.medicalConsumable)
            tvPayPrice.text = BigDecimalUtils.divide(item.payPrice)
            tvDelete.onClick {
                onCustomBottomClick?.onButtonCallBack(position, "delete")
            }
            tvWxCode.onClick {
                onCustomBottomClick?.onButtonCallBack(position, "wxCode")
            }
        }
    }

    override fun getViewBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): LayoutConsumableOrderItemBinding {
        return LayoutConsumableOrderItemBinding.inflate(layoutInflater, parent, false)
    }


    private fun getTitle(consumables: MutableList<MedicalConsumable>): String {
        var listName = ArrayList<String?>()
        for (item in consumables) {
            listName.add("${item.name}x${item.num}")
        }
        return listName.joinToString(separator = "/")
    }
}